﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using StockholmsStad.Web.UI.Class.Utilities;

namespace StockholmsStad.Web.UI.Controls
{
    /// <summary>
    /// Webcontrol för "step-meny"
    /// </summary>
    public partial class StepControl : UserControl
    {
        private readonly List<Step> _steps = new List<Step>();

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// Adds a step to the StepControl menu
        /// </summary>
        /// <param name="label"></param>
        /// <param name="selectedStep"></param>
        /// <param name="checkedStep"></param>
        public void AddStep(string label, 
                             bool selectedStep, 
                             bool checkedStep)
        {
            _steps.Add(new Step
                           {CheckedStep = checkedStep, 
                                    Label = label, 
                                    SelectedStep = selectedStep});
        }

        /// <summary>
        /// Clears all steps in step menu
        /// </summary>
        public void Clear()
        {
            _steps.Clear();
        }

        /// <summary>
        /// Redraws the current step menu
        /// </summary>
        public void Refresh()
        {
            StepsPlaceHolder.Controls.Clear();
            for ( int i = 0; i < _steps.Count; i++)
            {
                Step step = _steps[i];
                
                var span = new HtmlGenericControl("span");
                if (i == 0)
                {
                    var left = new HtmlGenericControl("div");    
                    left.Attributes.Add("class", step.SelectedStep ? "blueleft" : "grayleft");
                    span.Controls.Add(left);
                }

                var bg = new HtmlGenericControl("div");
                bg.Attributes.Add("class", step.SelectedStep ? "bluebg" : "graybg");
                span.Controls.Add(bg);

                var stepText = new HtmlGenericControl("span");
                stepText.InnerText = step.Label;
                stepText.Attributes.Add("class", step.CheckedStep
                                                     ?
                                                        "checked"
                                                     :
                                                         step.SelectedStep ? "not_checked_blue" : "not_checked");

                bg.Controls.Add(stepText);
                if (i + 1 == _steps.Count)
                {
                    var right = new HtmlGenericControl("div");    
                    right.Attributes.Add("class", step.SelectedStep ? "blueright" : "grayright");
                    span.Controls.Add(right);
                }
                else if (i + 1 < _steps.Count)
                {
                    var middle = new HtmlGenericControl("div");    
                    middle.Attributes.Add("class", step.SelectedStep ? _steps[i+1].SelectedStep ? "bluemiddle" : "bluemiddle" : 
                        _steps[i+1].SelectedStep ? "graybluemiddle" : "graymiddle");
                    span.Controls.Add(middle);
                }
                StepsPlaceHolder.Controls.Add(span);
            }           
            

        }
    }
}